package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.model.entity.pojo.video.CommentLike;
import com.lzw.breezeNovel.webApp.mapper.video.ShortCommentMapper;
import com.lzw.breezeNovel.webApp.mapper.video.VideoCommentMapper;
import com.lzw.breezeNovel.webApp.service.impl.common.AsyncService;
import com.lzw.breezeNovel.webApp.mapper.video.CommentLikeMapper;
import com.lzw.breezeNovel.webApp.service.video.CommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/24
 * @description 评论区点赞表
 */
@Service
@SuppressWarnings("all")
public class CommentLikeServiceImpl implements CommentLikeService {

    /**
     * 注入mapper
     */
    @Autowired
    private CommentLikeMapper commentLikeMapper;
    /**
     * 调用异步
     */
    @Autowired
    private AsyncService asyncService;
    /**
     * 注入评论表
     */
    @Autowired
    private VideoCommentMapper videoCommentMapper;
    /**
     * 注入短视频评论
     */
    @Autowired
    private ShortCommentMapper shortCommentMapper;

    /**
     * 评论区点赞
     * @param commentId
     * @param userId
     * @param type
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(Long commentId, Long userId, Integer type) {
        // 参数校验
        if (commentId == null || userId == null || type == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        //判断是否点赞过了
        Boolean islike = commentLikeMapper.selectByCommentIdAndUserIdAndType(commentId, userId, type) > 0;
        //判断是否点过赞
        if(islike)
        {
            throw new BaseException(500,"已经点赞过了");
        }
        //创建实体对象
        CommentLike commentLike = new CommentLike();
        //设置字段
        commentLike.setLikeTime(new Date());
        commentLike.setUserId(userId);
        commentLike.setType(type);
        commentLike.setCommentId(commentId);
        commentLikeMapper.insert(commentLike);
        //更新评论点赞数
        if(type == 3)
        {
            shortCommentMapper.updateLikeCountAdd(commentId,userId);
        }
        videoCommentMapper.updateLikeCountAdd(commentId,userId,type);
    }

    /**
     * 评论区取消点赞
     * @param commentId
     * @param userId
     * @param type
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unlike(Long commentId, Long userId, Integer type) {
        if (commentId == null || userId == null || type == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        commentLikeMapper.deleteByCommentIdAndUserIdAndType(commentId, userId, type);
        //更新评论点赞数
        if(type == 3)
        {
            shortCommentMapper.updateLikeCountSub(commentId,userId);
        }
        videoCommentMapper.updateLikeCountSub(commentId,userId,type);
    }
}
